#!/bin/bash
# version: 1.2
# date: 2022/05/31
# 参考: https://www.jianshu.com/p/cb0bb7031cf0

# 根据top中显示 CPU这一行，100-空闲id，计算或得

# 输出小数 printf "%s\n
topCpu=`top -b -n1 | grep "Cpu(s)" | tail -1 | awk -F'id,' '{split($1, vs, ","); v=vs[length(vs)]; sub(/\s+/, "", v);sub(/\s+/, "", v); printf "%s\n", 100-v; }'`

# 输出整数 printf "%d\n
#topCpu=`top -b -n1 | fgrep "Cpu(s)" | tail -1 | awk -F'id,' '{split($1, vs, ","); v=vs[length(vs)]; sub(/\s+/, "", v);sub(/\s+/, "", v); printf "%d\n", 100-v; }'`

echo " $topCpu"
